// pages/product.js
var WxParse = require('../../wxParse/wxParse.js');

//获取应用实例
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    width: null, // 屏幕宽度 
    pid: null,
    mid: null,
    result: [],
    indicatorDots: true, //是否显示面板指示点
    autoplay: true, //是否自动切换
    interval: 3000, //自动切换时间间隔,3s
    duration: 1000, //  滑动动画时长1s
    productNavtab: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '加载中...',
    })
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          width: res.windowWidth,
        });
      },
    })



    var that = this;
    that.setData({
      pid: options.pid,
      mid: options.mid,
    });
    that.getData(options.mid, options.pid);
  },
  // 数据
  getData: function (mid, pid) {
    console.log(mid + '  ' + pid);
    var that = this;
    var url = app.globalData.baseUrl + 'product/' + mid
    wx.request({
      url: url,
      method: 'POST',
      dataType: 'json',
      success: function (res) {
        var code = res.data.errorcode;
        console.log(res.data);
        if (code == app.globalData.success) {
          that.setData({
            result: res.data.data,
          });
          // html文件转换为小程序格式
          WxParse.wxParse('desc', 'html', res.data.data.desc, that, 5);
          WxParse.wxParse('rentDesc', 'html', res.data.data.rentDesc, that, 5);
        } else {
          wx.showToast({
            title: res.data.info,
          });
        }
      },
      fail: function (res) {
        wx.showToast({
          title: '服务开小差，请稍后再试！',
        });
      },
      complete: function (res) {
        wx.hideLoading();
      },
    })
  },
  // 租用
  rent: function (e) {
    var mid = e.currentTarget.dataset.mid;
    var url = app.globalData.baseUrl + 'order/create/' + mid;
    wx.request({
      url: url,
      method: 'POST',
      dataType: 'json',
      success: function (res) {
        console.log(res.data);
        var code = res.data.errorcode;
        if (code == app.globalData.success) {

        }
      },
      fail: function (res) {
        wx.showToast({
          title: '服务开小差，请稍后再试！',
          icon: 'error',
          duration: 2000
        });
      },
      complete: function (res) {
        wx.hideLoading();
        var code = res.data.errorcode;
        if (code == app.globalData.success) {

        } else if (code == app.globalData.forbidden) {
          wx.showModal({
            title: '',
            content: '需要注册成为会员并且交付押金才能进行租用哦！',
            cancelText: '取消',
            cancelColor: '#919191',
            confirmText: '注册',
            confirmColor: '#a72126',
            success: function (res) {
              if (res.confirm) {
                console.log('用户点击确定')
              }
            }
          })
        } else {
          wx.showToast({
            title: res.data.info,
            image: '../../images/icon_ts_1.png',
            duration: 2000,
            mask: true
          });
        }
      },
    })
  },

  //切换tab
  switchTab: function (e) {
    var that = this;
    that.setData({
      productNavtab: e.currentTarget.dataset.pdx
    });
  },

})